Ext.require([
    'Ext.form.field.ComboBox',
    'Ext.form.FieldSet',
    'Ext.tip.QuickTipManager',
    'Ext.toolbar.Paging',
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.util.*',
    'Ext.state.*'
]);

Ext.onReady(function() {
	Ext.QuickTips.init(); //为组件提供提示信息功能，form的主要提示信息就是客户端验证的错误信息。
	Ext.form.Field.prototype.msgTarget='side'; //提示的方式，枚举值为: 
	//结算方式
    var statementTypeEnum = new Ext.data.Store({
        singleton:true,
        autoDestroy:true,
        fields: ["value", "id"],
        proxy:{
            type:'ajax',
            url:jypath+'/marketing/haveReceiveRefund/statementTypeEnum',
            reader:{
                type: 'json', 
                root: 'root'  //数据
           }
        },
        autoLoad:true
    });
    
    //银行账号
    var getBankAccount = new Ext.data.Store({
        singleton:true,
        autoDestroy:true,
        fields: ["dictionaryName"],
        proxy:{
            type:'ajax',
            url:jypath+'/estate/dictionary/getDictionary?dictionaryType=BA',
            reader:{
                type: 'json', 
                root: 'root'  //数据
           }
        },
        autoLoad:true
    });
	
	var form_value = new Ext.form.FormPanel({
		title : "收支信息",
		region : 'north',
		frame : true,
		minHeight:150,
		defaults: {
			//width : 200
		},
		items:[
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "房&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号", value: combined_name, labelWidth:60, width:400},
					{ xtype: "displayfield", fieldLabel: "客户名称", value: customer_name, labelWidth:60}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "房款类型", value: relationType, labelWidth:60, width:400},
					{ xtype: "displayfield", fieldLabel: "业务标志", value: businessType, labelWidth:60}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "发&nbsp;&nbsp;生&nbsp;&nbsp;额", value: realAmount, labelWidth:60, width:400},
					{ xtype: "displayfield", fieldLabel: "结算日期", value: dateFormat(statementDate), labelWidth:60}
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "displayfield", fieldLabel: "结&nbsp;&nbsp;算&nbsp;&nbsp;人", value: statement_name, labelWidth:60, width:400},
					{ xtype: "displayfield", fieldLabel: "结算方式", value: paymentMethod_name, labelWidth:60}
	            ]
		    }
		 ]
	});
	
	var form = new Ext.form.FormPanel({
		title : "复核信息",
		region : 'north',
		frame : true,
		defaults: {
			//width : 200
		},
		items:[
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
					{ 
						xtype: "radiogroup",
						id:"result",
						fieldLabel: "复核结果",
						name :"m_result",
						width:200,
						labelWidth:60,
						items:[{
					    		boxLabel:'通过',
					    		inputValue:'1',
					    		name:'check',
					    		checked: true
					    	},{
					    		boxLabel:'退回',
					    		name:'check',
					    		inputValue:'0'
					    	}
					     ],
				            listeners:{
				            	'change':function(value){
				            		if(value.lastValue.check=='1'){
//				            			console.log(Ext.getCmp('checkRemark'));
				            			Ext.getCmp('checkRemark').hide();
				            			Ext.getCmp('checkRemark').allowBlank=true;
				            			Ext.getCmp('billCode').show();
				            			Ext.getCmp('billCode').allowBlank=false;
				            			Ext.getCmp('statementTypeE').show();
				            			Ext.getCmp('statementTypeE').allowBlank=false;
				            			Ext.getCmp('bankAccountE').show();
				            			Ext.getCmp('bankAccountE').allowBlank=false;
				            			Ext.getCmp('checkDate').show();
				            			Ext.getCmp('checkDate').allowBlank=false;
				            		}else{
				            			Ext.getCmp('checkRemark').show();
				            			Ext.getCmp('checkRemark').allowBlank=false;
				            			Ext.getCmp('billCode').hide();
				            			Ext.getCmp('billCode').allowBlank=true;
				            			Ext.getCmp('statementTypeE').hide();
				            			Ext.getCmp('statementTypeE').allowBlank=true;
				            			Ext.getCmp('bankAccountE').hide();
				            			Ext.getCmp('bankAccountE').allowBlank=true;
				            			Ext.getCmp('checkDate').hide();
				            			Ext.getCmp('checkDate').allowBlank=true;
				            		}
				            	}
				            }
					},
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	                { xtype: "textfield", fieldLabel: "票据号码", id: "billCode", name: "billCode", value: billCode, labelWidth:60,
		                allowBlank:false,
		                blankText:'不能为空',
	                    maxLength:30,
	                    maxLengthText:"票据号码最多30位！"
	                }
	            ]
		    },
		/*    {
				xtype: "container",
	            layout: "hbox",
	            items: [
	               {
		                xtype:'textarea',
		                fieldLabel: '退回原因',
		                name:'checkRemark',
		                allowBlank:false,
		                blankText:'不能为空', 
		                labelWidth:60,
		                hidden:true,
		                id:"checkRemark"
		            }
	            ]
		    },*/
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
        	        {  
	                  xtype:'combobox',
	 		    	  fieldLabel:'结算方式',
	 		    	  store:statementTypeEnum,
	 		    	  displayField: "value",
	 		    	  valueField:"id",
	 		    	  id:'statementTypeE',
	 		    	  queryMode:'local',
				      typeAhead: true,
				      labelWidth:60,
		                allowBlank:false,
		                blankText:'不能为空'
        	        },
        	        {  
  	                  xtype:'combobox',
  	 		    	  fieldLabel:'结算银行',
  	 		    	  store:getBankAccount,
  	 		    	  displayField: "dictionaryName",
  	 		    	  valueField:"dictionaryName",
  	 		    	  id:'bankAccountE',
  	 		    	  queryMode:'local',
  				      typeAhead: true,
  				      labelAlign:'right',
  				      width:341,
  				      labelWidth:90,
		                allowBlank:false,
		                blankText:'不能为空'
          	        }
	            ]
		    },
		    {
				xtype: "container",
	            layout: "hbox",
	            items: [
					{
					   xtype:'datefield',
					   format:'Y-m-d',
					   fieldLabel:'复核日期',
					   id:'checkDate',
					   labelWidth:60,
		                allowBlank:false,
		                blankText:'不能为空'
					}, 
	            ]
		    }
		 ],
		 dockedItems: [{
	            xtype: 'toolbar',
	            dock: 'bottom',
	            ui: 'footer',
	            layout: {
	                pack: 'left'
	            },
	            items: [{
	                minWidth: 80,
	                text: '保存',
	                id:'save_button',
	                handler:save_checked
	            },{
	                minWidth: 80,
	                text: '返回',
	                handler:backProjectDetail
	            }]
	      }]
	});
	
	var viewport = Ext.create('Ext.Viewport', {
		layout : 'border',
		items : [form_value, form]
	});
	
	function save_checked(){
		var resultForm = form.getForm();
      	if(resultForm.isValid()){
    		Ext.Ajax.request({
    			url: jypath+'/marketing/haveReceiveRefund/edit',
    			params:{
    				newBillCode:Ext.getCmp("billCode").getValue(),
    				statementType:Ext.getCmp("statementTypeE").getValue(),
    				bankAccount:Ext.getCmp("bankAccountE").getValue(),
    				result:Ext.getCmp("result").getValue(),
    				cdate:Ext.getCmp("checkDate").getValue(),
    				reChecked:"1",
    				id:re_id,
    				checkedFlag:"1"
    			},
    			callback: function (options, success, response) {
    				 if (success) {
    					 Ext.MessageBox.alert("系统提示","复核成功");  
						 setTimeout(function(){
							backProjectDetail();
						 }, 2000);
    	        	 }else{
    	        		 Ext.MessageBox.alert("系统提示","系统异常!");
    	        	 }
    			}
    			
    		});
      	}
	}
	
	function backProjectDetail(){
		location.href=jypath+"/marketing/haveReceiveRefund/index";
	}
	
	function dateFormat(value){
	    if(null != value && value!=""){
	        return Ext.Date.format(new Date(value),'Y-m-d'); 
	    }else{ 
	        return ""; 
	    } 
	} 
	
});
